/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.poemmanager.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.poemPoet.domain.PoemPoetComment;
import me.zhengjie.poemPoet.service.PoemPoetCommentService;
import me.zhengjie.poemUser.domain.PoemUserComment;
import me.zhengjie.poemUser.service.PoemUserCommentService;
import me.zhengjie.poemmanager.domain.Comment;
import me.zhengjie.poemmanager.domain.vo.CommentQueryCriteria;
import me.zhengjie.poemmanager.service.CommentService;
import me.zhengjie.post.domain.PostComment;
import me.zhengjie.post.service.PostCommentService;
import me.zhengjie.recitation.domain.RecitationComment;
import me.zhengjie.recitation.service.RecitationCommentService;
import me.zhengjie.utils.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author hwl
* @date 2024-12-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "comment管理")
@RequestMapping("/api/comment")
public class CommentController {

    private final CommentService commentService;
    private final PoemPoetCommentService poemPoetCommentService;
    private final PoemUserCommentService poemUserCommentService;
    private final RecitationCommentService recitationCommentService;
    private final PostCommentService postCommentService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @AnonymousAccess
    public void exportComment(HttpServletResponse response, CommentQueryCriteria criteria) throws IOException {
        commentService.download(commentService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询comment")
    @ApiOperation("查询comment")
    @AnonymousAccess
    public ResponseEntity<PageResult<Comment>> queryComment(CommentQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(commentService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping(value="/paged")
    @Log("分页查询comment")
    @ApiOperation("分页查询comment")
    @AnonymousAccess
    public ResponseEntity<PageResult<Comment>> pagedQueryComment(CommentQueryCriteria criteria,
                                                                 @RequestParam(defaultValue = "1") int current,
                                                                 @RequestParam(defaultValue = "20") int size  ){
        Page<Object> page = new Page<>(current,size);
        return new ResponseEntity<>(commentService.queryAll(criteria,page),HttpStatus.OK);
    }

    /**
     * 评论的地方的类别（0 post,1 poem_user,2 poem_poet,3 recitation）
     * @param resources
     * @return
     */
    @PostMapping
    @Log("新增comment")
    @ApiOperation("新增comment")
    @AnonymousAccess
    public ResponseEntity<Object> createComment(@Validated @RequestBody Comment resources) {
        switch (resources.getObjectType()) {
            case 0:
                postCommentService.create(convertToPostComment(resources));
                break;
            case 1:
                poemUserCommentService.create(convertToPoemUserComment(resources));
                break;
            case 2:
                poemPoetCommentService.create(convertToPoemPoetComment(resources));
                break;
            case 3:
                recitationCommentService.create(convertToRecitationComment(resources));
                break;
            default:
                throw new IllegalArgumentException("无效的评论类型: " + resources.getObjectType());
        }
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改comment")
    @ApiOperation("修改comment")
    @AnonymousAccess
    public ResponseEntity<Object> updateComment(@Validated @RequestBody Comment resources) {
        switch (resources.getObjectType()) {
            case 0:
                postCommentService.update(convertToPostComment(resources));
                break;
            case 1:
                poemUserCommentService.update(convertToPoemUserComment(resources));
                break;
            case 2:
                poemPoetCommentService.update(convertToPoemPoetComment(resources));
                break;
            case 3:
                recitationCommentService.update(convertToRecitationComment(resources));
                break;
            default:
                throw new IllegalArgumentException("无效的评论类型: " + resources.getObjectType());
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除comment")
    @ApiOperation("删除comment")
    @AnonymousAccess
    public ResponseEntity<Object> deleteComment(@RequestParam Integer type, @RequestBody List<Integer> ids) {
        switch (type) {
            case 0:
                postCommentService.deleteAll(ids);
                break;
            case 1:
                poemUserCommentService.deleteAll(ids);
                break;
            case 2:
                poemPoetCommentService.deleteAll(ids);
                break;
            case 3:
                recitationCommentService.deleteAll(ids);
                break;
            default:
                throw new IllegalArgumentException("无效的评论类型: " + type);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/my")
    @Log("查询我的评论")
    @ApiOperation("查询我的评论")
    @AnonymousAccess
    public List<Comment> queryMyComment(@RequestParam Integer type, @RequestParam String userName){
        switch (type) {
            case 0:
                return postCommentService.getMyComment(userName);
            case 1:
                return poemUserCommentService.getMyComment(userName);
            case 2:
                return poemPoetCommentService.getMyComment(userName);
            case 3:
                return recitationCommentService.getMyComment(userName);
            default:
                throw new IllegalArgumentException("无效的评论类型: " + type);
        }
    }

    // 转换方法组（返回具体子类）
    private PostComment convertToPostComment(Comment origin) {
        PostComment target = new PostComment();
        BeanUtils.copyProperties(origin, target, "objectType");
        return target;
    }

    private PoemUserComment convertToPoemUserComment(Comment origin) {
        PoemUserComment target = new PoemUserComment();
        BeanUtils.copyProperties(origin, target, "objectType");
        return target;
    }

    private PoemPoetComment convertToPoemPoetComment(Comment origin) {
        PoemPoetComment target = new PoemPoetComment();
        BeanUtils.copyProperties(origin, target, "objectType");
        return target;
    }

    private RecitationComment convertToRecitationComment(Comment origin) {
        RecitationComment target = new RecitationComment();
        BeanUtils.copyProperties(origin, target, "objectType");
        return target;
    }
}